<?php

class IndexController extends CustomControllerAction {

    public function indexAction() {

        $this->view->page_title = $this->seo_config['index_title'];
        $this->view->page_keywords = $this->seo_config['index_keywords'];
        $this->view->page_description = $this->seo_config['index_description'];
        if ($this->getRequest()->isPost()) {
            
            $_SESSION['filter_cat'] = $this->getRequest()->getPost('cats_filter');
            $_SESSION['filter_project'] = $this->getRequest()->getPost('projects_filter');
            $_SESSION['search'] = $this->getRequest()->getPost('search');
        }elseif (!$this->getRequest()->getParam('page')){
            $_SESSION['filter_cat'] = array();
            $_SESSION['filter_project'] = array();
            $_SESSION['search'] = '';
        }
        if (($_GET['sort']) && ($_GET['order'])) {
            $sort = $_GET['sort'];
            $order = $_GET['order'];
            $_SESSION['tender_sort'] = $sort . ' ' . $order;
        } elseif (!empty($_SESSION['tender_sort'])) {
            $temp = explode(' ', $_SESSION['tender_sort']);
            $sort = $temp['0'];
            $order = $temp['1'];
        } else {
            $sort = 'name';
            $order = 'asc';
            $_SESSION['tender_sort'] = $sort . ' ' . $order;
        }
        
        $per_page = $this->configuration['tenders_per_page'];
        $pages = ceil(DatabaseObject_Tender::GetFilterCount($this->db, array('idCat' =>  $_SESSION['filter_cat'], 'idProject' =>   $_SESSION['filter_project'], 'archive' => '0', 'search' => $_SESSION['search']), array('order' => $_SESSION['tender_sort'])) / $per_page);
        $page = max(1, $this->getRequest()->getParam('page'));
        $offset = ($page - 1) * $per_page;
        $tender = DatabaseObject_Tender::FilterTenders($this->db,  array('idCat' =>  $_SESSION['filter_cat'], 'idProject' =>   $_SESSION['filter_project'], 'archive' => '0', 'search' => $_SESSION['search']), array('order' => $_SESSION['tender_sort'], 'limit' => $per_page, 'offset' => $offset));


        $project = DatabaseObject_Project::GetProjects($this->db);
        $cats = DatabaseObject_Categories::GetCategories($this->db);

        $this->view->cats_filter = $_SESSION['filter_cat'];
        $this->view->projects_filter = $_SESSION['filter_project'];
        $this->view->tenders = $tender;
        $this->view->cats = $cats;
        $this->view->projects = $project;
        $this->view->sort = $sort;
        $this->view->order = $order;
        $this->view->page = $page;
        $this->view->pages = $pages;
    }

    public function archiveAction() {

        $this->view->page_title = $this->seo_config['archive_title'];
        $this->view->page_keywords = $this->seo_config['archive_keywords'];
        $this->view->page_description = $this->seo_config['archive_description'];
        if ($this->getRequest()->isPost()) {
            
            $_SESSION['filter_cat'] = $this->getRequest()->getPost('cats_filter');
            $_SESSION['filter_project'] = $this->getRequest()->getPost('projects_filter');
            $_SESSION['search'] = $this->getRequest()->getPost('search');
        }elseif (!$this->getRequest()->getParam('page')){
            $_SESSION['filter_cat'] = array();
            $_SESSION['filter_project'] = array();
            $_SESSION['search'] = '';
        }

        if (($_GET['sort']) && ($_GET['order'])) {
            $sort = $_GET['sort'];
            $order = $_GET['order'];
            $_SESSION['tender_sort'] = $sort . ' ' . $order;
        } elseif (!empty($_SESSION['tender_sort'])) {
            $temp = explode(' ', $_SESSION['tender_sort']);
            $sort = $temp['0'];
            $order = $temp['1'];
        } else {
            $sort = 'name';
            $order = 'asc';
            $_SESSION['tender_sort'] = $sort . ' ' . $order;
        }

        $per_page = $this->configuration['tenders_per_page'];
        $pages = ceil(DatabaseObject_Tender::GetFilterCount($this->db, array('idCat' =>  $_SESSION['filter_cat'], 'idProject' =>   $_SESSION['filter_project'], 'archive' => '1', 'search' => $_SESSION['search']), array('order' => $_SESSION['tender_sort'])) / $per_page);
        $page = max(1, $this->getRequest()->getParam('page'));
        $offset = ($page - 1) * $per_page;
        $tender = DatabaseObject_Tender::FilterTenders($this->db, array('idCat' =>  $_SESSION['filter_cat'], 'idProject' =>   $_SESSION['filter_project'], 'archive' => '1', 'search' => $_SESSION['search']), array('order' => $_SESSION['tender_sort'], 'limit' => $per_page, 'offset' => $offset));


        $project = DatabaseObject_Project::GetProjects($this->db);
        $cats = DatabaseObject_Categories::GetCategories($this->db);

       $this->view->cats_filter = $_SESSION['filter_cat'];
        $this->view->projects_filter = $_SESSION['filter_project'];
        $this->view->tenders = $tender;
        $this->view->cats = $cats;
        $this->view->projects = $project;
        $this->view->sort = $sort;
        $this->view->order = $order;
        $this->view->page = $page;
        $this->view->pages = $pages;
    }

    
    public function viewpageAction() {
        $alias = $this->getRequest()->getParam('alias');
        
        $page = new DatabaseObject_Pages($this->db);
        $page->load($alias, 'alias');
        
        $this->view->page = $page;
    }
    
    public function sendrequestcompleteAction() {
        
    }
}

?>
